<template>
  <div class="realtime-metrics">
    <div class="header">
      <h3 class="title">实时数据 ({{ timestamp }})</h3>
      <el-button
        type="primary"
        text
        size="small"
        @click="handleRefresh"
        :icon="Refresh"
      >
        刷新
      </el-button>
    </div>
    <div class="metrics-grid">
      <div v-for="(metric, index) in metrics" :key="index" class="metric-item">
        <h3 class="value">{{ metric.value }}</h3>
        <p class="label">
          <span class="icon" :class="metric.color"></span> {{ metric.label }}
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { Refresh } from "@element-plus/icons-vue";

const emit = defineEmits(["refresh"]);

defineProps({
  timestamp: {
    type: String,
    required: true,
  },
  metrics: {
    type: Array,
    required: true,
    validator: (metrics) => metrics.every((m) => m.value && m.label && m.color),
  },
});

const handleRefresh = () => {
  emit("refresh");
};
</script>

<style lang="scss" scoped>
.realtime-metrics {
  background-color: #fff;
  border-radius: 4px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  border: 1px solid #eaeaea;
  margin-bottom: 20px;

  .header {
    display: flex;
    align-items: center;
    margin-bottom: 27px;
    font-size: 14px;
    color: #666;
    .title {
      font-size: 16px;
      color: #333;
      font-weight: 500;
    }
  }

  .metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;

    .metric-item {
      text-align: center;
      padding: 8px;

      .value {
        font-weight: 400;
        font-size: 30px;
        color: #000000;
        line-height: 35px;
      }

      .label {
        font-weight: 400;
        font-size: 14px;
        color: #999999;
        line-height: 16px;
        margin-top: 10px;
        .icon {
          width: 10px;
          height: 10px;
          border-radius: 2px 2px 2px 2px;
          margin-right: 4px;
          display: inline-block;
          &.blue {
            background: #008df0;
          }
          &.orange {
            background: #f0a400;
          }
          &.green {
            background: #16ca46;
          }
          &.rate {
            background: #11c3cf;
          }
        }
      }
    }
  }
}
</style>